<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 项目
 */
class ProjectController extends Controller {
	//筛选
/*SELECT
	discount,
	`name`,
	most_save,
	count(1) AS num,
	hot
FROM
	project p
LEFT JOIN project_img i ON p.id = i.project_id
LEFT JOIN house ON house.project_id = p.id
WHERE
	house.state = 1 -- i.sort = 1
AND p.area = '".$_GET["area"]."'
AND p.id IN (
	SELECT
		project_id
	FROM
		(
			SELECT
				project_id
			FROM
				project_metro
			WHERE
				metro = '".$_GET["metro"]."'
			UNION ALL
				SELECT
					project_id
				FROM
					project_house_style
				WHERE
					house_style = '".$_GET["house_style"]."'
				UNION ALL
					SELECT
						project_id
					FROM
						project_price_region
					WHERE
						price_region = '".$_GET["price_region"]."'
		) allf
	GROUP BY
		project_id
	HAVING
		count(1) > 2
)
GROUP BY
	house.project_id
ORDER BY
	-- most_save DESC
	hot DESC 
	-- discount asc*/
	public function filter(){
		$sql = "select p.id,discount,name,most_save,count(1) as num,hot from project p left join project_img i on p.id=i.project_id LEFT JOIN house ON house.project_id = p.id where p.flag=1 and house.state = 1";
		// $_GET["area"];$_GET["house"];$_GET["metro"];$_GET["price"];
		// $_GET["sort"]
		
		if(isset($_GET["area"])){
			$sql = $sql." AND p.area = '".$_GET["area"]."'";  
		}
		$filter = 0;
		$time = 0;
		if(isset($_GET["house"])){
			$time++;
			$sql = $sql." and p.id in( select project_id from (";
			$filter = 1;
			$sql = $sql." select project_id from project_house_style where house_style = '".$_GET["house"]."'";  
		}
		if(isset($_GET["metro"])){
			$time++;
			if($filter == 0){
				$sql = $sql." and p.id in( select project_id from (";
				$filter = 1;
			}else{
				$sql = $sql." union all";
			}
			$sql = $sql." select project_id from project_metro where metro = '".$_GET["metro"]."'";  
		}
		if(isset($_GET["price"])){
			$time++;
			if($filter == 0){
				$sql = $sql." and p.id in( select project_id from (";
				$filter = 1;
			}else{
				$sql = $sql." union all";
			}
			$sql = $sql." select project_id from project_price_region where price_region = '".$_GET["price"]."'";  
		}
		if($time != 0){
			$sql = $sql.") allf GROUP BY project_id HAVING count(1) = ".$time." )";
		}
		$sql = $sql." GROUP BY house.project_id";
		$alllist = M()->query($sql);
		$num = ceil(count($alllist)/6);
		if($_GET["sort"]=="price"){
			$sql = $sql." order by most_save desc";
		}elseif($_GET["sort"]=="discount"){
			$sql = $sql." order by discount asc";
		}else{
			$sql = $sql." order by hot desc";
		}
		//分页
		//$_GET["p"]
		if(isset($_GET["p"])){
			$first = (intval($_GET["p"])-1)*6;
			$sql = $sql." limit ".$first.",6";
		}else{
			$sql = $sql." limit 0,6";
		}
		$list = M()->query($sql);
		$data["num"] = $num;
		$data["list"]= $list;
		echo json_encode($data);
	}
}
?>